<template>
  <div class="tenantMenu_main main_container add_container">
    <div v-loading="loading" class="main_main">
      <div class="SearchBar_main main_main_search">
        <div class="main_main_search_item">
          <div class="formItem_header">
            <div class="formItem_header_operate">
              <a class="formItem_header_operate_items" @click="add">
                <svg-icon icon-class="新增" />
                <div class="item_name">新增</div>
              </a>
            </div>
          </div>
        </div>
        <div class="main_main_search_item">
          <el-input v-model="pageData.searchValue" style="width:200px;margin-right:10px" placeholder="请输入仓库编码或名称" clearable @keyup.enter.native="getList" @clear="getList" />
          <el-button style="background:#387bfc;width:30px;" icon="el-icon-search" @click.stop="getList" />
        </div>
      </div>
      <div ref="table_container" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"

          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          @sort-change="(column)=>{sortChange(column, this)}"
          @row-click="singleElection"
        >
          <el-table-column label="" width="65">
            <template slot-scope="scope">
              <!--这里的{{ "" }}为了隐藏索引值 -->
              <el-radio
                v-model="templateSelection"
                class="radio"
                :label="scope.row.id"
              >{{ '' }}</el-radio>
            </template>
          </el-table-column>
          <Column v-for="(column,index) in defaultTableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" />
        </el-table>
      </div>
    </div>
    <div class="main_bottoms">
      <el-checkbox v-model="checked">默认其他物料也入该仓库</el-checkbox>
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>
    <div class="main_bottom">
      <div>
        <el-button type="cancel" @click.stop="cancel">取消</el-button>
        <el-button @click.stop="onSubmit">确定</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { list } from '@/api/equipmentManage/sparePart/warehouse'
import AddOrUpdate from './addOrUpdate' // 新增或编辑页面

export default {
  name: 'Index',
  props: {
    layerid: {},
    sparePartId: {},
    warehouseId: {}
  },
  data() {
    return {
      loading: true,
      pageId: '备件仓库', // 页面唯一标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc',
        searchValue: '',
        status: 1
      },
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'code', label: '仓库编码' },
        { prop: 'name', label: '仓库名称' },
        { prop: 'remark', label: '备注' }
      ],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'code', label: '仓库编码', type: 'input' },
        { prop: 'name', label: '仓库名称', type: 'input' }
      ],

      operationData: [ // 右上角的操作按钮
        { label: '新增', perm: '' },
        { label: '批量删除', perm: '' },
        { label: '批量导出', perm: '' },
        { label: '页面配置', perm: '' }
      ],
      templateSelection: undefined,
      selectionsRow: {},
      checked: false
    }
  },
  mounted() {
    // this.defaultSearchColumns[1].options = this.statusOptions // 给 defaultSearchColumns 赋值
    this.getList()
    this.templateSelection = this.warehouseId
    this.$nextTick(() => {
      this.tableHeight = this.$refs.table_container.offsetHeight
    })
  },
  methods: {
    getList() {
      list(this.pageData).then(res => {
        this.tableData = res.data
        this.total = res.total
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
        })
      })
    },
    // 单选
    singleElection(row) {
      this.selectionsRow = row
      this.templateSelection = row.id
    },
    add() {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
          }// props
        },
        area: ['460px', '350px'],
        title: '新增仓库', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    cancel() {
      this.$layer.close(this.layerid)// 关闭弹框
    },
    onSubmit() {
      if (Object.keys(this.selectionsRow).length == 0 && !this.warehouseId) {
        this.$message.warning('请选择仓库')
        return false
      } else if (Object.keys(this.selectionsRow).length == 0 && this.warehouseId) { // 说明是修改仓库
        this.tableData.forEach(item => {
          if (item.id == this.warehouseId) {
            this.selectionsRow = item
          }
        })
      }
      this.$parent.showWarehouse(this.selectionsRow, this.checked)

      this.$layer.close(this.layerid)// 关闭弹框
    }
  }
}
</script>

<style lang="less" scoped>
  .main_bottoms{
    width: 100%;
    height: 50px;
    background-color: white;
    padding: 0px 30px;
    display: flex;justify-content:space-between;align-items: center;
  }
  .formItem_header_operate_items{
  height: 30px;
  display: flex;justify-content: center;align-items: center;
  padding: 0px 10px;font-size: 16px;border-radius: 4px;
  color: rgba(56, 123, 252, 1);background-color: rgba(235, 242, 255, 1);
  .item_name{
    font-size: 14px;padding-left: 10px;
  }
}
.formItem_header_operate_items:hover{
  background-color: rgba(235, 242, 255, 0.5);
}
.main_bottom{
  padding-right: 20px !important;
}
.main_search_item{
  padding-right: 20px !important;
}
.main_main_search_item{
  padding-left: 20px !important;
}
.main_main{
  margin: 0 !important;
  padding-top:0 !important ;
}
</style>
